<?php
class APPCore_Form_BaseForm extends Zend_Form
{
    /**
     * List elements outline default in layout
     * @var array
     */
    private $_defaultOutline = array(
        'Zend_Form_Element_Textarea',
        'Zend_Form_Element_Hidden',
        'APPCore_Form_Element_Table',
        'APPCore_Form_Element_Ckeditor'
    );

    private $_specialDecorator = array(
        'Zend_Form_Element_File',
        'APPCore_Form_Element_Description'
    );

    public function init()
    {
        parent::init();
    }

    public static function getExtImage()
    {
        return array('jpg', 'png', 'gif', 'bmp');
    }

    public static function getExtMedia()
    {
        return array('mp3', 'mp4', 'flv', 'wma', 'avi', 'mpg');
    }

    public static function getExtDocument()
    {
        return array('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx');
    }

    public static function getExtCompress()
    {
        return array('zip');
    }

    public static function getExtAllType()
    {
        return array(
            'jpg', 'png', 'gif', 'bmp',
            'mp3', 'mp4', 'flv', 'wma', 'avi', 'mpg',
            'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
            'zip'
        );
    }

    public static function getMaximumSize()
    {
        return 2047;
    }

    /**
     * decorator some tags of html in form to tag table
     * decorator form, group, elememts....
     */
    public function decorator()
    {
        //set decorators for form
        $this->setDecorators(array(
            'FormElements',
            array(array('data' => 'HtmlTag'),
            array(
                'tag' => 'table',
                'class' => 'zend-form')),
            'Form'
        ));

        $this->_setDecoratorSubForm($this);

        //Get elements of form and add decorator
        $elements = $this->getElements();
        foreach ($elements as $element) {
            $this->addDecoratorFormElement($element);
        }

        //set decorator for groups
        $this->_setDecoratorFormGroups();
    }

    /**
     * set decorator form for groups
     */
    private function _setDecoratorFormGroups()
    {
        $this->setDisplayGroupDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table', 'class' => 'zend-form-table-group')),
            array('Fieldset', array('class' => 'zend_form_group')),
            array(
                array('data' => 'HtmlTag'),
                array('tag' => 'td', 'colspan' => '2')
            ),
            array(
                array('row' => 'HtmlTag'),
                array('tag' => 'tr', 'class' => 'zend-form-row-group')
            ),
        ));
    }

    /**
     * set decorator for all sub form
     */
    private function _setDecoratorSubForm($form)
    {
        $subForms = $form->getSubForms();
        foreach ($subForms as $subForm) {
            //add decorator for sub form
            $subForm->addDecorator(new APPCore_Form_Decorator_SubForm());

            //recursive set decorator for sub form
            $this->_setDecoratorSubForm($subForm);

            //add decorator for elements in sub form
            foreach ($subForm->getElements() as $element) {
                $this->addDecoratorFormElement($element);
            }
        }

        return;
    }

    /**
     * Add decorator for all element in form
     * @param Zend_Form_Element | APPCore_Form_Element $element
     */
    public function addDecoratorFormElement($element)
    {
        //Get type element
        $typeElement = $element->getType();

        //Set outline default
        $outline = (in_array($typeElement, $this->_defaultOutline) ? true : false);

        //Set attribute outline
        $attribOutline = $element->getAttrib('outline');
        if (!is_null($attribOutline)) {
            $outline = $element->getAttrib('outline');
        }

        //Add decorator for element
        if (!in_array($element->getType(), $this->_specialDecorator)) {
            if ($outline) {
                $element->addDecorator(new APPCore_Form_Decorator_Outline());
            } else {
                $element->addDecorator(new APPCore_Form_Decorator_Inline());
            }
        }

        //Add decorector for some special elements
        switch ($element->getType()) {
            case 'Zend_Form_Element_File': {
                    $element->addDecorator(new APPCore_Form_Decorator_File());
                    break;
                }
            case 'APPCore_Form_Element_Description': {
                    $element->addDecorator(new APPCore_Form_Decorator_Description());
                    break;
                }
            case 'Zend_Form_Element_Hidden': {
                    $element->addDecorator(new APPCore_Form_Decorator_Hidden());
                    break;
                }
            case 'APPCore_Form_Element_Render': {
                    $element->addDecorator(new APPCore_Form_Decorator_Hidden());
                    break;
                }
            case 'Zend_Form_Element_Button': {
                    $element->addDecorator(new APPCore_Form_Decorator_Button());
                    break;
                }
            case 'Zend_Form_Element_Reset': {
                    $element->addDecorator(new APPCore_Form_Decorator_Button());
                    break;
                }
            case 'Zend_Form_Element_Submit': {
                    $element->addDecorator(new APPCore_Form_Decorator_Submit());
                    break;
                }
        }
    }

    /**
     * @desc Remove decorate for form element
     */
    protected function removeDecorationForElements()
    {
        $elemens = $this->getElements();
        if (is_array($elemens) && count($elemens) > 0) {
            foreach ($elemens as $ele) {
                if (is_object($ele)) {
                    $ele->removeDecorator('label')->removeDecorator('HtmlTag');
                }
            }
        }
    }
}